def html = new File("index2.html").text;
html = html.replaceAll(/\n/,' ').replaceAll(/\s+/,' ').toLowerCase();
//print html;

def imagePattern = /<.*?src="([^"]*?\.(gif|jpg|png|bmp))".*?>/; ///<.*?src="([^"]*?\.(gif|jpg|png|bmp))".*?\/?>/;
def cssEmbededImagePattern = /url\((.*?)\)/;

def cssPattern = /<link.*?href="([^"]*?css)".*?\/>/;
def scriptPattern = /<script.*?src="([^"]*?)".*?>/;
def swfPattern = /(http:\/\/www.bcn.(cat|es)\/[^\.]*?\.swf)/;

def cssArr = [];
def imageArr = [];
def scriptArr = [];
def swfArr = [];


def resourceResult = "";

appendResourceIfMatch(html,imagePattern,imageArr,resourceResult);
appendResourceIfMatch(html,cssEmbededImagePattern,imageArr,resourceResult);

appendResourceIfMatch(html,cssPattern,cssArr,resourceResult);
appendResourceIfMatch(html,scriptPattern,scriptArr,resourceResult);
appendResourceIfMatch(html,swfPattern,swfArr,resourceResult);

def appendResourceIfMatch(html,pattern,arr,index = 1,resourceResult){
	html.eachMatch(pattern){ match->
		def originalSource = match[index];
		originalSource = originalSource.replace("#","");
		if(originalSource!=""){
			def resource = new PageResource(originalSource);
			resource.sysc();
			println resource;
			arr << resource;
		}
	}
}


def createFolderIfNotExist(fileName){
	
}

class GlobalConfig{
	public static String rootSitePortal = "http://www.bcn.cat/";
	public static String rootPathPattern = /http\:\/\/.*?\.bcn\.(es|cat)\//;
	public static String currentHandlePage = "http://www.bcn.cat/english/ihome.htm";
	public static String currentPageRelativePathToRoot="../";
}


class PageResource{
	String siteUrl;
	String localUrl;

	public PageResource(String src){
		

		def siteUrl = src.replaceAll(/\.\.\//, GlobalConfig.rootSitePortal);
		this.siteUrl = siteUrl;
		this.localUrl = this.switchResourcePath(src);
		
	}

	
	def switchResourcePath(path){
		def rootpathPattern = GlobalConfig.rootPathPattern;
		return path.replaceAll(rootpathPattern,GlobalConfig.currentPageRelativePathToRoot).replace("#","");
	}
	
	def String toString(){
		return siteUrl + " | " + localUrl;
	}
	
	def sysc(){
		def dirPath = new File(this.localUrl).getParent();
		new File(dirPath).mkdirs();
		try
		{
			this.download(this.siteUrl,dirPath);
		}
		catch(Exception ex){}
	}

	def download(url,localPath)
	{
		def localResPath = localPath + "/" + url.tokenize("/")[-1];
		println "localResPath:" + localResPath;
		/* def file = new FileOutputStream(new FileOutputStream(new File(localResPath)));
		def out = new BufferedOutputStream(file);
		out << new URL(url).openStream();
		out.close(); */
		
		def file = new File(localResPath);
		 use (FileBinaryCategory)
		 {
		      file << this.siteUrl.toURL();
		 }
	}
}

class FileBinaryCategory
{
  def static leftShift(File a_file, URL a_url)
  {
    def input
    def output

    try
    {
      input = a_url.openStream()
      output = new BufferedOutputStream(new FileOutputStream(a_file))

      output << input
    }
    finally
    {
       input?.close()
       output?.close()
    }
  }
}



def result = html=~imagePattern;
//print result[0];

def temp = new File("temp.txt");

temp.text = resourceResult;